package com.hjm.mall.api;

import com.hjm.mall.config.annotation.TokenToMallUserId;
import com.hjm.mall.entity.Image;
import com.hjm.mall.service.impl.ImageServiceImpl;
import com.hjm.mall.util.Result;
import com.hjm.mall.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/api/image")
@Api(value = "image",tags = "哈基米商城——图片接口")
public class ImageController {
    @Autowired
    private ImageServiceImpl imageService;

    //从数据库查询获得json数据
    @GetMapping("/queryAll")
    @ApiOperation(value = "查询全部图片", notes = "返回json")
    public Result<String> queryAll(@TokenToMallUserId @ApiParam(hidden = true,name = "用户id")Long userId){

        Result result = ResultGenerator.genSuccessResult("查询成功！");
        result.setData(imageService.findAllImage());
        return result;
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增图片", notes = "需要token验证")
    public Result<String> insert(
            @ApiParam("图片文件（支持PNG、JPEG、GIF）")
            @RequestParam("image")
                    MultipartFile file,
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId){
        Result result = null;

        if (file==null) {
            return ResultGenerator.genErrorResult(404,"找不到文件！");
        }
        Image newImage = imageService.prepImageSaveToDB(file);
        if(newImage!=null){
            if(imageService.insertImage(newImage)>0){
                result = ResultGenerator.genSuccessResult("上传成功！");
                result.setData(newImage.getId());
            }
        }
        return result;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查找图片", notes = "不需要token验证")
    public ResponseEntity<byte[]> getImageById(
            @ApiParam("图片id")
            @PathVariable("id")
                    Integer id) {
        Image imageById = imageService.getImageById(id);
        if (imageById == null) {
            // 如果找不到图片，返回一个404响应
            return ResponseEntity.notFound().build();
        }
        return imageService.prepareImageResponseEntity(imageById);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新图片", notes = "需要token验证")
    public Result<String> update(
            @RequestParam("image")
            @ApiParam("图片文件（支持PNG、JPEG、GIF）")
                    MultipartFile file,
            @ApiParam("图片id")@RequestParam("id")
                    Integer id,
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId){
        Result result = null;

        if(file == null){
            return ResultGenerator.genErrorResult(404,"找不到文件！");
        }
        if(id == null || id<0){
            return ResultGenerator.genErrorResult(404,"id不正确！");
        }
        Image newImage = imageService.prepImageSaveToDB(file);
        if(newImage!=null){
            newImage.setId(id);
            if(imageService.updateImage(newImage)>0){
                result =   ResultGenerator.genSuccessResult("图片修改成功");
                result.setData(newImage.getId());
                return  result;
            }
        }
        result = ResultGenerator.genFailResult("图片修改失败！");
        return result;
    }

    @GetMapping("/del")
    @ApiOperation(value = "删除图片", notes = "需要token验证")
    public Result<String> deleteImageById(
            @ApiParam("图片id")
            @RequestParam("id")
                    Integer id,
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId){

        if (id==null){
            return ResultGenerator.genErrorResult(404,"id不正确！");
        }

        return ResultGenerator.genSuccessResult(imageService.delImage(id));
    }
}
